package com.my.okhttps.utils.security;

import com.alibaba.fastjson.JSONObject;
import com.my.okhttps.utils.security.util.AesUtil;
import com.my.okhttps.utils.security.util.RSAUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wyz02
 * @version 1.0
 * @ClassName SecurityUtil
 * @Description: TODO
 * @date 2021/1/27 16:59
 */
public class SecurityUtil {
    /*
    **
     * @Author Wuyuzhong
     * @Description 加密
     * @Date 2021/1/27 17:23
     * @Param [paramIn, publicKeyStr]业务数据  rsa公钥
     * @return java.util.Map<java.lang.String,java.lang.String>
     **/
    public static Map<String, String> encrypt(Map<String, String> paramIn, String publicKeyStr){
        Map<String, String> resultMap = new HashMap<>();
        //获取aes加密参数
        Map<String, String> aesOut = AesUtil.getAesParam();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = df.format(new Date())+"";
        aesOut.put("systime",time);
        //aes加密业务数据
        String paramInStr = AesUtil.encryptCbcMode(JSONObject.toJSONString(paramIn), aesOut.get("aesKey"), aesOut.get("aesIV"));


        //公钥加密
        String encryptedData = null;
        try {
            //对aes加密参数进行rsa公钥加密
            encryptedData = RSAUtil.encryptByPublicKey(JSONObject.toJSONString(aesOut),publicKeyStr);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("rsa加密失败");

        }
        encryptedData = encryptedData.replaceAll("\\+","%2B");
        paramInStr = paramInStr.replaceAll("\\+","%2B");
        //业务数据入参命名为 paramIn
        resultMap.put("paramIn",paramInStr);
        //aes加密信息参数名为paramAes
        resultMap.put("paramAes",encryptedData);
        return resultMap;
    }


    /*
    **
     * @Author Wuyuzhong
     * @Description 解密
     * @Date 2021/1/27 17:32
     * @Param [dataMap, privateKeyStr]返回值data，rsa私钥
     * @return java.lang.String
     **/
    public static String decrypt(Map dataMap, String privateKeyStr){


        //业务数据密文
        String decryptedData = dataMap.get("valueData").toString();

        //aes加密信息 密文，需rsa解密
        String aesstr = dataMap.get("paramAes").toString();
        aesstr = aesstr.replaceAll("%2B","+");
        decryptedData = decryptedData.replaceAll("%2B","+");
        String valueDataStr = null;
        try {
            valueDataStr = RSAUtil.decryptByPrivateKey(aesstr, privateKeyStr);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("rsa解密失败");
        }

        System.out.println(decryptedData);
        System.out.println("--收到服务端返回AES加密参数--"+valueDataStr);
        //用收到的aes加密参数对业务数据解密
        //Map map6 = JSONObject.parseObject(JSONObject.toJSONString(valueDataStr), Map.class);
        Object object = JSONObject.parseObject(valueDataStr);
        Map<String, String> map6 = JSONObject.parseObject(JSONObject.toJSONString(object), Map.class);
        //Map map6 = strToMap(valueDataStr);

        String tempParam = AesUtil.decryptCbcMode(decryptedData, map6.get("aesKey"), map6.get("aesIV"));


        return tempParam;
    }
}
